package com.hck.taozi_help.module.yuyin;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.hck.taozi_help.utils.LogUtil;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.SynthesizerListener;

public class KeDaYuYingPlayVoice {
    public static String voicerCloud = "xiaoyan";
    // 默认本地发音人
    public static String voicerLocal = "xiaoyan";

    private SpeechSynthesizer mTts;
    private SynthesizerListener mTtsListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {
            //showTip("开始播放");
            LogUtil.log("onSpeakBegin: ");
        }

        @Override
        public void onSpeakPaused() {
            LogUtil.log("onSpeakPaused: ");
        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {

        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {

        }

        @Override
        public void onCompleted(SpeechError speechError) {
            if (speechError!=null){
                LogUtil.log("onCompleted: "+speechError.toString());
            }
            else {
                LogUtil.log("onCompleted: ");
            }

        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {

        }
    };

    private void setParam(Context context) {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        //设置合成
       // if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
            //设置使用云端引擎
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
            //设置发音人
            mTts.setParameter(SpeechConstant.VOICE_NAME, voicerCloud);

//        } else if (mEngineType.equals(SpeechConstant.TYPE_LOCAL)) {
//            //设置使用本地引擎
       //     mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
//            //设置发音人资源路径
        //    mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath(context));
//            //设置发音人
         //   mTts.setParameter(SpeechConstant.VOICE_NAME, voicerLocal);
//        } else {
//            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_XTTS);
//            //设置发音人资源路径
//            mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
//            //设置发音人
//            mTts.setParameter(SpeechConstant.VOICE_NAME, voicerXtts);
//        }
        //mTts.setParameter(SpeechConstant.TTS_DATA_NOTIFY,"1");//支持实时音频流抛出，仅在synthesizeToUri条件下支持
        //设置合成语速
        mTts.setParameter(SpeechConstant.SPEED, "50");
        //设置合成音调
        mTts.setParameter(SpeechConstant.PITCH,"50");
        //设置合成音量
        mTts.setParameter(SpeechConstant.VOLUME, "50");
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        //	mTts.setParameter(SpeechConstant.STREAM_TYPE, AudioManager.STREAM_MUSIC+"");

        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
                context.getExternalFilesDir("msc").getAbsolutePath() + "/tts.pcm");


    }

    //获取发音人资源路径
    private String getResourcePath(Context context) {
        StringBuffer tempBuffer = new StringBuffer();
        String type = "tts";
//        if (mEngineType.equals(SpeechConstant.TYPE_XTTS)) {
//            type = "xtts";
//        }
        //合成通用资源
        tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, type + "/common.jet"));
        tempBuffer.append(";");
        //发音人资源
      //  if (mEngineType.equals(SpeechConstant.TYPE_XTTS)) {
          //  tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, type + "/" + voicerXtts + ".jet"));
       // } else {
            tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, type + "/" + voicerLocal + ".jet"));
       // }

        return tempBuffer.toString();
    }

    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            LogUtil.log("onInitonInit: "+code);
            if (code != ErrorCode.SUCCESS) {

            } else {
                mTts.startSpeaking(voiceData, mTtsListener);
            }
        }
    };
    private String voiceData;
    public  void playVoice(Context context,String text){
        voiceData=text;
        if (mTts==null){
            mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
            setParam(context);
        }
        else {
            mTts.startSpeaking(voiceData, mTtsListener);
        }

    }
}
